package com.example.sqlite.activities;

import com.example.sqlite.dao.ContactsDAO;
import com.example.sqlite.models.Contact;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ShowContactActivity extends Activity {

	private ContactsDAO dao;
	private long idContact;
	private Contact contact;
	private Button buttonDelete;
	private Button buttonEdit;
	private TextView textViewName;
	private TextView textViewFone;
	private AlertDialog.Builder dialog;

	private final android.content.DialogInterface.OnClickListener listenerButtonOk = new android.content.DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface arg0, int arg1) {
			dao.delete(contact);
			finish();
		}
	};

	private final OnClickListener onClickListenerButtonDelete = new OnClickListener() {

		@Override
		public void onClick(View view) {
			dialog = new AlertDialog.Builder(ShowContactActivity.this);
			dialog.setTitle("Excluir?");
			dialog.setMessage("Tem certeza que deseja excluir?");
			dialog.setPositiveButton("ok", listenerButtonOk);
			dialog.setNegativeButton("N�o", null);
			dialog.show();
		}
	};
	private final OnClickListener onClickListenerButtonEdit = new OnClickListener() {

		@Override
		public void onClick(View view) {
			Intent intent = new Intent(ShowContactActivity.this,
					EditContactActivity.class);
			intent.putExtra("id", contact.getId());
			startActivity(intent);
			finish();
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show_contact);

		dao = new ContactsDAO(this);
		dao.open();

		Intent intent = getIntent();

		idContact = intent.getExtras().getLong("id");
		contact = dao.get(idContact);

		buttonDelete = (Button) findViewById(R.id.button_del);
		buttonEdit = (Button) findViewById(R.id.button_edit);
		textViewName = (TextView) findViewById(R.id.text_view_name_show);
		textViewFone = (TextView) findViewById(R.id.text_view_fone_show);

		textViewName.setText(contact.getName());
		textViewFone.setText(contact.getFone());

		buttonDelete.setOnClickListener(onClickListenerButtonDelete);
		buttonEdit.setOnClickListener(onClickListenerButtonEdit);
	}

	@Override
	protected void onResume() {
		dao.open();
		super.onResume();
	}

	@Override
	protected void onPause() {
		dao.close();
		super.onPause();
	}

}
